package com.yinhai.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.lz.util.SystemParsUtils;
import com.lz.util.Utils;
import com.yinhai.adapter.SystemSetupMainAdpter;
import com.yinhai.constant.Contact;
import com.yinhai.util.Util;

public class SystemSetupMainActivity extends Activity {
    private ImageView btn_back;
    
    private ListView listView;
    
    List<String> list = new ArrayList<String>();
    
    private SystemSetupMainAdpter systemSetupMainAdpter;
    
    SystemSetupMainActivity thisRef = null;
    
    private List<String> dialSelect = new ArrayList<String>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.systemsetupmain);
        thisRef = this;
        // 返回
        btn_back = (ImageView)findViewById(R.id.btn_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                onBackPressed();
            }
        });
        listView = (ListView)findViewById(R.id.listview);
        systemSetupMainAdpter = new SystemSetupMainAdpter(SystemSetupMainActivity.this);
        if (list != null) {
            list.clear();
        }
        list.add("安全设置");
        list.add("清除所有群聊信息");
        list.add("信息提示音");
        list.add("通讯录更新频率");
        list.add("设置启动界面");
        list.add("短信签名");
        list.add("拨号界面快速搜索范围");
        systemSetupMainAdpter.assignment(list);
        listView.setAdapter(systemSetupMainAdpter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        // 安全设置
                        Intent it0 = new Intent(SystemSetupMainActivity.this, SystemSetupActivity.class);
                        startActivity(it0);
                        break;
                    case 1:
                        // 清除本地记录
                        Intent it1 = new Intent(SystemSetupMainActivity.this, SystemSetupClearTalkInfoActivity.class);
                        startActivity(it1);
                        break;
                    case 2:
                        // 声音提示
                        Intent it2 = new Intent(SystemSetupMainActivity.this, SystemSetupSoundActivity.class);
                        startActivity(it2);
                        break;
                    case 3:
                        // 单位通讯录
                        int v = SystemParsUtils.getInstance(SystemSetupMainActivity.this).getInt("contactUpate");
                        new AlertDialog.Builder(SystemSetupMainActivity.this).setTitle("通讯录更新频率")
                            .setSingleChoiceItems(new String[] {Contact.ONE_DAY, Contact.ONE_WEEK, Contact.ONE_MONTH},
                                v,
                                new DialogInterface.OnClickListener() {
                                    
                                    @Override
                                    public void onClick(DialogInterface arg0, int arg1) {
                                        SystemParsUtils.getInstance(SystemSetupMainActivity.this)
                                            .setInt("contactUpate", arg1);
                                        arg0.dismiss();
                                    }
                                })
                            .show();
                        break;
                    // 应用启动位置
                    case 4:
                        int index = SystemParsUtils.getInstance(SystemSetupMainActivity.this).getInt("mainIndex",1);
                        new AlertDialog.Builder(SystemSetupMainActivity.this).setTitle("设置启动界面")
                            .setSingleChoiceItems(new String[] {"拨号界面", "消息", "通讯录", "集团应用"},
                                index,
                                new DialogInterface.OnClickListener() {
                                    
                                    @Override
                                    public void onClick(DialogInterface arg0, int arg1) {
                                        SystemParsUtils.getInstance(SystemSetupMainActivity.this).setInt("mainIndex",
                                            arg1);
                                        arg0.dismiss();
                                    }
                                })
                            .show();
                        break;
                    case 5:
                        final EditText editText =
                            (EditText)getLayoutInflater().inflate(R.layout.setting_message_sign, null);
                        editText.setText(SystemParsUtils.getInstance(SystemSetupMainActivity.this)
                            .getString("messageSign"));
                        new AlertDialog.Builder(SystemSetupMainActivity.this).setTitle("短信签名")
                            .setView(editText)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    SystemParsUtils.getInstance(SystemSetupMainActivity.this).setString("messageSign",
                                        editText.getText().toString());
                                    arg0.dismiss();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    
                                }
                            })
                            .show();
                        break;
                    case 6:
                        dialSelect.clear();
                        dialSelect.add("0");
                        dialSelect.add("1");
                        boolean[] booleans = new boolean[] {true, true};
                        int value = SystemParsUtils.getInstance(SystemSetupMainActivity.this).getInt("dialSearch",-1);
                        if (value == 0) {
                            booleans[1] = false;
                            dialSelect.remove("1");
                        } else if (value == 1) {
                            booleans[0] = false;
                            dialSelect.remove("0");
                        }
                        Builder builder = new AlertDialog.Builder(SystemSetupMainActivity.this);
                        builder.setTitle("拨号界面快速搜索范围")
                            .setMultiChoiceItems(new String[] {"手机通讯录", "集团通讯录"},
                                booleans,
                                new DialogInterface.OnMultiChoiceClickListener() {
                                    
                                    @Override
                                    public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
                                        if (arg2) {
                                            dialSelect.add(Integer.valueOf(arg1).toString());
                                        } else {
                                            dialSelect.remove(Integer.valueOf(arg1).toString());
                                        }
                                    }
                                })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    if (dialSelect.size() == 0) {
                                        Util.showMsg(SystemSetupMainActivity.this, "请选择搜索范围");
                                        return;
                                    }
                                    if (dialSelect.size() == 2) {
                                        SystemParsUtils.getInstance(SystemSetupMainActivity.this).setInt("dialSearch",
                                            -1);
                                    } else {
                                        SystemParsUtils.getInstance(SystemSetupMainActivity.this).setInt("dialSearch",
                                            Integer.valueOf(dialSelect.get(0)));
                                    }
                                    
                                    arg0.dismiss();
                                }
                            })
                            .show();
                        
                }
            }
        });
        
    }
    
    /**
     * 改变单位通讯录设置,保存到配置文件
     * */
    public OnCheckedChangeListener cbAutoLoginCheckedChanged = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Utils.SaveDWTXL(thisRef, isChecked);
            if (isChecked) {
                // Utils.startMsgService(thisRef);
                Utils.showToast(thisRef, "单位通讯录自动同步已开起");
            } else {
                // Utils.stopMsgService(thisRef);
                Utils.showToast(thisRef, "单位通讯录自动同步已关闭");
            }
        }
    };
    
    /*
     * @Override public boolean dispatchKeyEvent(KeyEvent event) { if(event.getAction
     * ()==KeyEvent.ACTION_DOWN&&event.getKeyCode()==KeyEvent.KEYCODE_BACK &&event.getRepeatCount()==0) {
     * onBackPressed(); return true; } return super.dispatchKeyEvent(event); }
     */
}
